﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using unvell.ReoGrid;
using shop.Bll.Logi;
using CommonTools;
using CommonTools.Controls;
using shop.Model;

namespace shop.Logi
{
    public partial class LogiInfoForm : DockContent
    {

        shop.Bll.BasicInfo.LogiInfoBll bll = new shop.Bll.BasicInfo.LogiInfoBll();


        public LogiInfoForm()
        {
            InitializeComponent();
        }

        private async void btn_Query_Click(object sender, EventArgs e)
        { 

            var list = await bll.Query(null);
            new GridDataBuilder(this.grid)
                .UseData(list)
                .UseFilter()
                .SetReadOnly(true)
                .End();

            if (list.Any()== false)
            {
                MessageBox.Show("没有查询到数据", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            var form = new LogiInfoEditDialog();
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.btn_Query_Click(null, null);
            }
        }

        private async void btn_modify_Click(object sender, EventArgs e)
        {
            var id = this.grid[cl_id, grid.CurrentCell.Row].IsNull(0);
            if (id <=0)
            {
                MessageBox.Show("请选择一项!", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var form = new LogiInfoEditDialog();
            form.Item = await bll.GetItem(id);

            if (form.ShowDialog() == DialogResult.OK)
            {
                this.btn_Query_Click(null, null);
            }
        }

        private async void btn_del_Click(object sender, EventArgs e)
        {
            var id = this.grid[cl_id, grid.CurrentCell.Row].IsNull(0);
            if (id <= 0)
            {
                MessageBox.Show("请选择一项!", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var res = await bll.Delete(id);
            if (res.IsOk == false)
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageTip.ShowOk(this, "已删除");
                this.btn_Query_Click(null, null);
            }

        }

        private void LogiInfoForm_Load(object sender, EventArgs e)
        {
            this.btn_Query_Click(null, null);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.btn_modify_Click(sender, e);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.btn_del_Click(sender, e);
        }

        private async void btn_copy_Click(object sender, EventArgs e)
        {
            var id = this.grid[cl_id, grid.CurrentCell.Row].IsNull(0);
            if (id <= 0)
            {
                MessageBox.Show("请选择一项!", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var item= await bll.GetItem(id);
            var newItem = item.Clone();
            newItem.id = 0;


            var form = new LogiInfoEditDialog();
            form.Item = newItem;

            if (form.ShowDialog() == DialogResult.OK)
            {
                this.btn_Query_Click(null, null);
            }
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.btn_copy_Click(sender, e);
        }

        private void btn_queryFromWdt_Click(object sender, EventArgs e)
        {
            //从旺店通查询
            var rr =WaitUI.Run( async ()=>  
            {
                var res = await new Bll.Logi.QueryLogisticsBll().QuerylogisticsCompany();
                if (res != null)
                {
                    var list = res
                        .Select(a => new LogiInfo()
                        {
                            id = 0,
                            IsAgent = a.logistics_name.Contains("代发"),
                            isdel = false,
                            ItemId = 1,
                            LogiAcct = string.Empty,
                            Remarks = a.remark,
                            logistics_id = a.logistics_id,
                            logistics_no= a.logistics_no,
                            logistics_name = a.logistics_name,
                            LogiCompany= a.LogiCompany,
                        })
                        .ToList();

                    //同步
                    await bll.SyncTo(list);
                    return list;
                }

                return null;
            });


            //重新查询
            this.btn_Query_Click(null, null);

            //new GridDataBuilder(this.grid)
            //    .UseData(rr)
            //    .NotAdjustWidth()
            //    .UseFilter()
            //    //.SetReadOnly(true)
            //    .End();
        }

        private void grid_CellEndEdit(object sender, CellEndEditEventArgs e)
        {
            if (e.Cell.Column == cl_LogiAcct.Index && e.Cell.Row>0)
            {
                ModifyLogiInfo(e.Cell.Row, grid[e.Cell].IsNull());
            }
        }

        /// <summary> 修改快递老板 </summary>
        public async void ModifyLogiInfo(int row, string name)
        {
            var id = grid[cl_id.Index, row].IsNull(0);
            if (id<=0)
            {
                return;
            }

            if (string.IsNullOrEmpty(name) &&
                MessageBox.Show("确认要删除快递老板名称?", Properties.Resources.systemInformation, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return;
            }

            await bll.ModifyLogiInfoName(id, name);
        }

        private void btn_Import_Click(object sender, EventArgs e)
        {

        }

        private void btn_downloadTemplate_Click(object sender, EventArgs e)
        {

        }
    }
}
